﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PennApps
{
    class PerformanceInfoToTextConversionEngine
    {
        public string Convert(ProcessPerformanceInformation[] performanceInformationArray)
        {
            string EmailBody = "";
            foreach (ProcessPerformanceInformation val in performanceInformationArray)
            {
                if (val != null)
                {
                    EmailBody += val.ProcessName + ";" + val.MemoryUsage + ";" + val.CPUUsage + ";" + val.TotalIOCount + ";" + val.ModuleLocation + "\n";
                }
            }
            return EmailBody;
        }
        public ProcessPerformanceInformation[] ConvertBack(string dataText)
        {
            string[] tempArray = dataText.Split('\n').Where<string>(str => str.Replace("\r", "").Replace("\n", "").Length > 0).ToArray();
            string[] dataArray;

            //tempArray = dataText.Split('\n');
            ProcessPerformanceInformation[] perfArray = new ProcessPerformanceInformation[tempArray.Length];
            for (int i = 0; i < (tempArray.Length - 1); i++)
            {
                perfArray[i] = new ProcessPerformanceInformation();
                dataArray = tempArray[i].Split(';');
                perfArray[i].ProcessName = dataArray[0];
                perfArray[i].MemoryUsage = dataArray[1];
                perfArray[i].CPUUsage = dataArray[2];
                perfArray[i].TotalIOCount = dataArray[3];
                perfArray[i].ModuleLocation = dataArray[4];
            }
            return perfArray;
        }
        public Command CommandConvert(string emailText)
        {
            Command action = new Command();
            string[] tempArray = emailText.Split(';').Where<string>(str => str.Replace("\r", "").Replace("\n", "").Length > 0).ToArray();

            action.Action = (CommandAction)Enum.Parse(typeof(CommandAction), tempArray[0]);
            action.ProcessName = tempArray[1];
            action.ModuleLocation = tempArray[2];

            return action;
        }
        public string CommandConvertBack(Command commandText)
        {
            string commandEmail = "";
            commandEmail += commandText.Action + ";" + commandText.ProcessName + ";" + commandText.ModuleLocation;
            return commandEmail;
        }
    }
}